<?php namespace App\Http\Controllers\user\uc\third;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use App\Http\Controllers\user\uc\CommonController;
use Input;
use Validator;
use DB;
use App\Models\ThirdpartyBase;
use App\Models\ThirdpartyIndustry;
use App\Models\ThirdpartyProfessionalList;
use App\Models\ThirdpartyDevice;
use App\Models\Users;
use Session;





class ValidatorController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */

	public function __construct()
	{
		$this->middleware('isattestation');
	}
	public function index()
	{
		$type = env('THIRD');
		return view("user.uc.third.add_groupinfo")->withParentPos($this->getRoleName($type))->withSitePos("基本信息")->withType($type)->withProvinces(CommonController::getAllProvince()->toArray());
	}

	public function validatorIndustry()
	{
		$type = env('THIRD');
		return view("user.uc.third.add_industryinfo")->withParentPos($this->getRoleName($type))->withSitePos("基本信息")->withIndustryPos("行业信息")->withType($type);
	}

	public function saveIndustry()
	{
		$groupData = Input::get("groupData");

		$profession = Input::get("profession");
		$device = Input::get("device");
		$uid = $this->getUid();
		$model = ThirdpartyBase::where("uid", $uid);
		if(!$model->count())
		{
			$groupData["uid"] = $uid;
			DB::transaction(function() use ($groupData, $profession, $device, $uid){
				try{
					$imgArr[] = $groupData["enterprice_pic"];
					$imgArr[] = $groupData["group_code_img"];
					$imgArr[] = $groupData["regist_copy"];
					$imgArr[] = $groupData["logo"];
					$this->moveImages($imgArr, env("OTHERS_DIR"));
					ThirdpartyBase::create($groupData);
					$industry = ThirdpartyIndustry::create(["uid" => $uid]);
					$profession["industry_id"] = $industry->id;
					ThirdpartyProfessionalList::create($profession);
					$device["industry_id"] = $industry->id;
					ThirdpartyDevice::create($device);
					Users::where("id", $uid)->update(["role_id" => env("THIRD")]);
					$uSession = Users::with(['profile'])->where("id", $uid)->get()[0]->toArray();
					Session::put("user", $uSession);
				}catch(\ValidationException $e){
					Log::error($e);
				}
			});

			return $this->json([]);
		}
		else
		{
			return;
		}

	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
